﻿using Models.MVC;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Web.Common
{
    public class Common
    {
        public static  Models.Common.InitInfo GetInitInfoModel(int UserID)
        {
            Models.Common.InitInfo info = new Models.Common.InitInfo();
            info.homeInfo = new Models.Common.HomeInfoModel();
            info.homeInfo.href = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='HomeHref'").ToString();
            info.homeInfo.title = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='HomeTitle'").ToString();
            info.logoInfo = new Models.Common.LogoInfoModel();
            info.logoInfo.href = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='LogoHref'").ToString();
            info.logoInfo.image = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='LogoIcon'").ToString();
            info.logoInfo.title = FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='LogoTitle'").ToString();
            info.menuInfo = new List<Models.Common.ChildModel>();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from dbo.Fn_Table_MenuList("+UserID+",0) order by showindex").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                Models.Common.ChildModel m = new Models.Common.ChildModel();
                m.title = item["Title"].ToString();
                m.target = "_self";
                m.icon = item["Icon"].ToString();
                m.href = item["Href"].ToString();
                m.child = new List<Models.Common.ChildModel>();
                Web.Common.Common.AddMenuChild(m, Convert.ToInt32(item["ID"]),UserID);
                if (m.child.Count > 0)
                {
                    info.menuInfo.Add(m);
                }
            }
            return info;
        }
        public static void AddMenuChild(Models.Common.ChildModel m,int ID,int UserID)
        {
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from dbo.Fn_Table_MenuList(" + UserID.ToString() + ","+ID.ToString()+") order by showindex").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                Models.Common.ChildModel mm = new Models.Common.ChildModel();
                mm.title = item["Title"].ToString();
                mm.target = "_self";
                mm.icon = item["Icon"].ToString();
                mm.href = item["Href"].ToString();
                mm.child = new List<Models.Common.ChildModel>();
                AddMenuChild(mm, Convert.ToInt32(item["ID"]),UserID);
                if (mm.child.Count > 0 || !string.IsNullOrWhiteSpace(mm.href))
                {
                    m.child.Add(mm);
                }
            }
        }
        public static int IsAuthoritys(int AuthorityID)
        {
            int UserID = CookiesManagement.GetUserModel().UserID;
            return Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle(@"  select count(*)
  from RoleAuthority as a inner join Roles as b on a.RoleID=b.ID inner join UserRoles as c on b.ID=c.RoleID
  where c.UserID=" + UserID + " and a.AuthorityID=" + AuthorityID));
        }

        public static JiHuaOrderModel GetJiHuaOrderModel(int ID,int BillNameID)
        {
            JiHuaOrderModel m = new JiHuaOrderModel();
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select a.ID,a.BillNameID,a.Code,a.GoodsID,b.Name as GoodsName,b.Code as GoodsCode,b.Model as GoodsModel
		,a.Number,a.HeTongCode,CONVERT(nvarchar(10),OrderDate,120) as OrderDate,a.XuFangDaiHao,a.DengJi,a.WaiXing
		,a.HeTongQty,a.JiHuaQty,CONVERT(nvarchar(10),JiaoFuDate,120) as JiaoFuDate,GuiFanHao,JianZhiYanShou,WenJianBianHao,QiTa
from PlannedOrder as a inner join Goods as b on a.GoodsID=b.ID
where a.ID=" + ID + " and BillNameID=" + BillNameID + @"
").Tables[0];
            foreach (DataRow item in dt.Rows)
            {
                m.ID = Convert.ToInt32(item["ID"]);
                m.BillNameID = Convert.ToInt32(item["BillNameID"]);
                m.Code = Convert.ToString(item["Code"]);
                m.GoodsID = Convert.ToInt32(item["GoodsID"]);
                m.GoodsName = Convert.ToString(item["GoodsName"]);
                m.GoodsCode = Convert.ToString(item["GoodsCode"]);
                m.GoodsModel = Convert.ToString(item["GoodsModel"]);
                m.Number = Convert.ToString(item["Number"]);
                m.HeTongCode = Convert.ToString(item["HeTongCode"]);
                m.OrderDate = Convert.ToString(item["OrderDate"]);
                m.XuFangDaiHao = Convert.ToString(item["XuFangDaiHao"]);
                m.DengJi = Convert.ToString(item["DengJi"]);
                m.WaiXing = Convert.ToString(item["WaiXing"]);
                m.HeTongQty = Convert.ToDecimal(item["HeTongQty"]);
                m.JiHuaQty = Convert.ToDecimal(item["JiHuaQty"]);
                m.JiaoFuDate = Convert.ToString(item["JiaoFuDate"]);
                m.GuiFanHao = Convert.ToString(item["GuiFanHao"]);
                m.JianZhiYanShou = Convert.ToString(item["JianZhiYanShou"]);
                m.WenJianBianHao = Convert.ToString(item["WenJianBianHao"]);
                m.QiTa = Convert.ToString(item["QiTa"]);
            }

            DataTable gdt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select a.ID,a.BillNameID,a.BillID,a.Type,a.GoodsID,b.Name as GoodsName,b.Code as GoodsCode,b.Model as GoodsModel
		,a.Qty,a.LingQty,a.HuoYuanHao,a.JianYanPiHao,a.ZhiZhaoChangShang,a.XinPianMianJi,a.ZhengMianJinShu,a.BeiMianJinShu
from BillBom as a inner join Goods as b on a.GoodsID=b.ID
where a.BillID=" + ID + " and BillNameID=" + BillNameID + @"
").Tables[0];
            int i = 1;
            foreach (DataRow item in gdt.Rows)
            {
                if (Convert.ToInt32(item["Type"]) == 1)
                {
                    m.ZhuoGoodsCode = Convert.ToString(item["GoodsCode"]);
                    m.ZhuoGoodsModel = Convert.ToString(item["GoodsModel"]);
                    m.ZhuoGoodsID = Convert.ToInt32(item["GoodsID"]);
                    m.ZhuoGoodsName = Convert.ToString(item["GoodsName"]);
                    m.ZhuoHuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                    m.ZhuoJianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                    m.ZhuoLingQty = Convert.ToInt32(item["LingQty"]);
                    m.ZhuoQty = Convert.ToInt32(item["Qty"]);
                    m.ZhuoZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);

                }
                if (Convert.ToInt32(item["Type"]) == 2)
                {
                    m.MaoGoodsCode = Convert.ToString(item["GoodsCode"]);
                    m.MaoGoodsModel = Convert.ToString(item["GoodsModel"]);
                    m.MaoGoodsID = Convert.ToInt32(item["GoodsID"]);
                    m.MaoGoodsName = Convert.ToString(item["GoodsName"]);
                    m.MaoHuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                    m.MaoJianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                    m.MaoLingQty = Convert.ToInt32(item["LingQty"]);
                    m.MaoQty = Convert.ToInt32(item["Qty"]);
                    m.MaoZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);

                }
                if (Convert.ToInt32(item["Type"]) == 3)
                {
                    if (i == 1)
                    {
                        m.Xin1GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin1GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin1GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin1GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin1HuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                        m.Xin1JianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                        m.Xin1LingQty = Convert.ToInt32(item["LingQty"]);
                        m.Xin1Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin1ZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);
                        m.Xin1ZhengMianJinShu = Convert.ToString(item["ZhengMianJinShu"]);
                        m.Xin1BeiMianJinShu = Convert.ToString(item["BeiMianJinShu"]);
                        m.Xin1XinPianMianJi = Convert.ToString(item["XinPianMianJi"]);

                    }
                    if (i == 2)
                    {
                        m.Xin2GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin2GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin2GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin2GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin2HuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                        m.Xin2JianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                        m.Xin2LingQty = Convert.ToInt32(item["LingQty"]);
                        m.Xin2Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin2ZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);
                        m.Xin2ZhengMianJinShu = Convert.ToString(item["ZhengMianJinShu"]);
                        m.Xin2BeiMianJinShu = Convert.ToString(item["BeiMianJinShu"]);
                        m.Xin2XinPianMianJi = Convert.ToString(item["XinPianMianJi"]);

                    }
                    if (i == 3)
                    {
                        m.Xin3GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin3GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin3GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin3GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin3HuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                        m.Xin3JianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                        m.Xin3LingQty = Convert.ToInt32(item["LingQty"]);
                        m.Xin3Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin3ZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);
                        m.Xin3ZhengMianJinShu = Convert.ToString(item["ZhengMianJinShu"]);
                        m.Xin3BeiMianJinShu = Convert.ToString(item["BeiMianJinShu"]);
                        m.Xin3XinPianMianJi = Convert.ToString(item["XinPianMianJi"]);

                    }
                    if (i == 4)
                    {
                        m.Xin4GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin4GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin4GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin4GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin4HuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                        m.Xin4JianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                        m.Xin4LingQty = Convert.ToInt32(item["LingQty"]);
                        m.Xin4Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin4ZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);
                        m.Xin4ZhengMianJinShu = Convert.ToString(item["ZhengMianJinShu"]);
                        m.Xin4BeiMianJinShu = Convert.ToString(item["BeiMianJinShu"]);
                        m.Xin4XinPianMianJi = Convert.ToString(item["XinPianMianJi"]);

                    }
                    if (i == 5)
                    {
                        m.Xin5GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin5GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin5GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin5GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin5HuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                        m.Xin5JianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                        m.Xin5LingQty = Convert.ToInt32(item["LingQty"]);
                        m.Xin5Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin5ZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);
                        m.Xin5ZhengMianJinShu = Convert.ToString(item["ZhengMianJinShu"]);
                        m.Xin5BeiMianJinShu = Convert.ToString(item["BeiMianJinShu"]);
                        m.Xin5XinPianMianJi = Convert.ToString(item["XinPianMianJi"]);

                    }
                    if (i == 6)
                    {
                        m.Xin6GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin6GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin6GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin6GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin6HuoYuanHao = Convert.ToString(item["HuoYuanHao"]);
                        m.Xin6JianYanPiHao = Convert.ToString(item["JianYanPiHao"]);
                        m.Xin6LingQty = Convert.ToInt32(item["LingQty"]);
                        m.Xin6Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin6ZhiZhaoChangShang = Convert.ToString(item["ZhiZhaoChangShang"]);
                        m.Xin6ZhengMianJinShu = Convert.ToString(item["ZhengMianJinShu"]);
                        m.Xin6BeiMianJinShu = Convert.ToString(item["BeiMianJinShu"]);
                        m.Xin6XinPianMianJi = Convert.ToString(item["XinPianMianJi"]);

                    }
                    i = i + 1;
                }
            }
            return m;
        }

        public static JiHuaOrderModel GetJiHuaOrderModelForGoods(int GoodsID, int BillNameID)
        {
            JiHuaOrderModel m = new JiHuaOrderModel();
            DataTable gdt = new DataTable();
            if (BillNameID == 3)
            {
                gdt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select a.*,b.Name as GoodsName,b.Code as GoodsCode,b.Model as GoodsModel
from BillName2GoodBOM as a inner join Goods as b on a.GoodsID=b.ID
where a.MainGoodsID=" + GoodsID + @"
").Tables[0];
            }
            if (BillNameID == 6)
            {
                gdt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select a.*,b.Name as GoodsName,b.Code as GoodsCode,b.Model as GoodsModel
from BillName6GoodBOM as a inner join Goods as b on a.GoodsID=b.ID
where a.MainGoodsID=" + GoodsID + @"
").Tables[0];
            }
            int i = 1;
            foreach (DataRow item in gdt.Rows)
            {
                if (Convert.ToInt32(item["Type"]) == 1)
                {
                    m.ZhuoGoodsCode = Convert.ToString(item["GoodsCode"]);
                    m.ZhuoGoodsModel = Convert.ToString(item["GoodsModel"]);
                    m.ZhuoGoodsID = Convert.ToInt32(item["GoodsID"]);
                    m.ZhuoGoodsName = Convert.ToString(item["GoodsName"]);
                    m.ZhuoHuoYuanHao = "";
                    m.ZhuoJianYanPiHao = "";
                    m.ZhuoLingQty = 0;
                    m.ZhuoQty = Convert.ToInt32(item["Qty"]);
                    m.ZhuoZhiZhaoChangShang = "";

                }
                if (Convert.ToInt32(item["Type"]) == 2)
                {
                    m.MaoGoodsCode = Convert.ToString(item["GoodsCode"]);
                    m.MaoGoodsModel = Convert.ToString(item["GoodsModel"]);
                    m.MaoGoodsID = Convert.ToInt32(item["GoodsID"]);
                    m.MaoGoodsName = Convert.ToString(item["GoodsName"]);
                    m.MaoHuoYuanHao = "";
                    m.MaoJianYanPiHao = "";
                    m.MaoLingQty = 0;
                    m.MaoQty = Convert.ToInt32(item["Qty"]);
                    m.MaoZhiZhaoChangShang = "";

                }
                if (Convert.ToInt32(item["Type"]) == 3)
                {
                    if (i == 1)
                    {
                        m.Xin1GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin1GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin1GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin1GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin1HuoYuanHao = "";
                        m.Xin1JianYanPiHao = "";
                        m.Xin1LingQty = 0;
                        m.Xin1Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin1ZhiZhaoChangShang = "";
                        m.Xin1ZhengMianJinShu = "";
                        m.Xin1BeiMianJinShu = "";
                        m.Xin1XinPianMianJi = "";

                    }
                    if (i == 2)
                    {
                        m.Xin2GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin2GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin2GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin2GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin2HuoYuanHao = "";
                        m.Xin2JianYanPiHao = "";
                        m.Xin2LingQty = 0;
                        m.Xin2Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin2ZhiZhaoChangShang = "";
                        m.Xin2ZhengMianJinShu = "";
                        m.Xin2BeiMianJinShu = "";
                        m.Xin2XinPianMianJi = "";


                    }
                    if (i == 3)
                    {
                        m.Xin3GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin3GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin3GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin3GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin3HuoYuanHao = "";
                        m.Xin3JianYanPiHao = "";
                        m.Xin3LingQty = 0;
                        m.Xin3Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin3ZhiZhaoChangShang = "";
                        m.Xin3ZhengMianJinShu = "";
                        m.Xin3BeiMianJinShu = "";
                        m.Xin3XinPianMianJi = "";


                    }
                    if (i == 4)
                    {
                        m.Xin4GoodsCode = Convert.ToString(item["GoodsCode"]);
                        m.Xin4GoodsModel = Convert.ToString(item["GoodsModel"]);
                        m.Xin4GoodsID = Convert.ToInt32(item["GoodsID"]);
                        m.Xin4GoodsName = Convert.ToString(item["GoodsName"]);
                        m.Xin4HuoYuanHao = "";
                        m.Xin4JianYanPiHao = "";
                        m.Xin4LingQty = 0;
                        m.Xin4Qty = Convert.ToInt32(item["Qty"]);
                        m.Xin4ZhiZhaoChangShang = "";
                        m.Xin4ZhengMianJinShu = "";
                        m.Xin4BeiMianJinShu = "";
                        m.Xin4XinPianMianJi = "";


                    }
                    i = i + 1;
                }
            }
            return m;
        }

    }
}